/*
 * @Descripttion: GGB
 * @version: 1.0
 * @Author: GGB
 * @Date: 2021-08-13 06:57:46
 * @LastEditors: GGB
 * @LastEditTime: 2021-08-16 18:23:48
 */
import 'package:flutter/material.dart';
import 'package:backdrop/backdrop.dart';
import 'package:flutter_data_center/controller/common_controller.dart';
import 'package:flutter_data_center/controller/light_controller.dart';
import 'package:get/get.dart';
import 'package:scroll_to_index/scroll_to_index.dart';
import 'components/light_list.dart';
import 'components/light_statistics.dart';
import 'components/menu_grid.dart';
import 'components/menu_list.dart';

class LightScreen extends StatefulWidget {
  const LightScreen({Key key}) : super(key: key);
  static String routeName = "/light";

  @override
  _LightScreenState createState() => _LightScreenState();
}

class _LightScreenState extends State<LightScreen> {
  LightController _lightController = Get.put(LightController());
  CommonController _commonController = Get.put(CommonController());
  int currentIndex = 0;
  AutoScrollController controller;

  @override
  void initState() {
    super.initState();
    // 发起数据请求
    _lightController.getLightStatistics();
    if (_lightController.lightList.length == 0) {
      _lightController.getLights();
    }
    if (_commonController.floors.length == 0) {
      _commonController.getFloors();
    }
    // 初始化移动菜单控制器
    controller = AutoScrollController(
      keepScrollOffset: true,
      viewportBoundaryGetter: () => Rect.fromLTRB(
          MediaQuery.of(context).padding.left,
          0,
          MediaQuery.of(context).padding.right,
          0),
      axis: Axis.horizontal,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: SafeArea(
        child: Column(
          children: [
            LightStatistics(),
            Expanded(
                child: BackdropScaffold(
              headerHeight: 58,
              backgroundColor: Colors.white,
              appBar: BackdropAppBar(
                title: new Builder(
                  builder: (BuildContext context) => MenuList(
                    currentIndex: currentIndex,
                    controller: controller,
                    onChange: (value) {
                      if (!Backdrop.of(context).isBackLayerConcealed) {
                        Backdrop.of(context).fling();
                      }
                      _scrollToIndex(value);
                    },
                  ),
                ),
                actions: <Widget>[
                  BackdropToggleButton(
                      // icon: AnimatedIcons.list_view,
                      ),
                ],
                automaticallyImplyLeading: false,
              ),
              backLayer: new Builder(
                builder: (BuildContext context) => MenuGrid(
                  currentIndex: currentIndex,
                  onChange: (value) {
                    Backdrop.of(context).fling();
                    _scrollToIndex(value);
                  },
                ),
              ),
              frontLayer: Container(color: Colors.white, child: LightList()),
            )),
          ],
        ),
      ),
    );
  }

  Future _scrollToIndex(int index) async {
    setState(() {
      currentIndex = index;
    });

    await controller.scrollToIndex(index,
        duration: Duration(milliseconds: 500),
        preferPosition: AutoScrollPosition.begin);
  }
}
